# -*- coding:utf-8 -*-
import Pluto,struct,codecs,fnmatch,re

def makestr(i):
        string=[]
        j=0
        while j>=0:
                if re.match('//end',lines[i+1+j]):
                        break
                string.append(lines[i+1+j])
                j+=1
        return ''.join(string)[:-2]

fp=codecs.open('0000-00000200-0582.bin.txt','rb','utf16')
lines=fp.readlines()
fp.close()
line_num=len(lines)

tar=open('ff.bin','wb')
text_list=[]
for i in xrange(line_num):
        if re.match('//start',lines[i]):
                text=makestr(i)
                text_list.append(text)
num=len(text_list)
tar.write(struct.pack('i',num))
baseoffset=num*4+4
tar.seek(baseoffset)
offsetlist=[]
for text in text_list:
        text=text.replace('\r','').replace('[0d00]','\r')
        tar.write(text.encode('utf16')[2:])
        tar.write('\x00\x00')
        offsetlist.append(tar.tell()-baseoffset)
tar.seek(4)
for offset in offsetlist:
        tar.write(struct.pack('i',offset))
tar.close()